    <template>
  <main-layout>
    <div class="bodys">
      <div class="headers">
        <input  v-on:keyup.13="search" type="text" v-model="val" placeholder="搜索书名、分类、ISBN" />
        <img @click="search"  src="../../images/sousou.jpeg" alt="" class="photos" />
        <!-- <div>搜索</div> -->
      </div>
      <div class="container">
        <div class="booksList" v-for="item in searchArr" :key="item.title">
          <img :src="item.src" alt="" />
          <div class="box-right">
            <div class="title">{{ item.title }}</div>
            <div class="price">￥{{ item.price }}</div>
            <button class="bth" @click="addCart(item)">加入购物车</button>
          </div>
        </div>
      </div>
    </div>
  </main-layout>
</template>

<script>
import MockService from "../../apis/Ajax";
  
export default {
  data() {
    return {
      searchArr:JSON.parse(sessionStorage.getItem("search")),
      val:JSON.parse(sessionStorage.getItem("val")),
    };
  },
  methods:{
       async search(){
       let good= await MockService.searchBook(this.val)
       this.searchArr=good.data
       sessionStorage.setItem('search',JSON.stringify(this.searchArr))
       sessionStorage.setItem('val',JSON.stringify(this.val))
    //    this.$router.push("/search");
 
    },
 
      addCart(item){

     var obj={
        
        id:item.id,
        src:item.src,
        title:item.title,
        price:item.price,
        num:1,
        oneValue:false
     }
     let open=false
      let cartM=JSON.parse(sessionStorage.getItem('cartlist'))||[]
      console.log(cartM);
      this.cartList=cartM
      //  for(item in cartM){
      //    if(item.title==obj.title){
      //       open=true
      //       return
      //    }
      //  }
      let index=''
      for(let i=0;i<cartM.length;i++){
        if(cartM[i].title==obj.title){
          open=true
          index=i
        }
      }
      if(!open){
                this.cartList.push(obj)
          }else{
             this.cartList[index].num++
          }
          sessionStorage.setItem('cartlist',JSON.stringify(this.cartList))
       
     
       this.$router.push("/cart");
    }
  
  }
   
};

</script>
<style lang="less" scoped>
.headers {
  display: flex;
  position: relative;
  .photos {
    width: 30px;
    height: 30px;
    position: absolute;
    left: 315px;
    top: 22px;
  }
  input {
    width: 330px;
    height: 35px;
    margin-left: 20px;
    margin-top: 20px;
  }
  div {
    display: flex;
    flex-flow: column;
    font-size: 0.75rem;
    margin-left: 15px;
    margin-top: 25px;
    img {
      width: 20px;
      height: 20px;
      margin-left: 2px;
    }
  }
}
.container {
  width: 100%;
  overflow-x: hidden;
  display: block;
  height: 100%;
  max-width: 100%;
  .booksList {
    width: 100%;
    display: flex;
    margin-top: 50px;
    font-size: 0.9375rem;

    img {
      height: 150px;
      border-radius: 15px;
    }
    .box-right {
      width: 100%;
      overflow: hidden;
      background-color: #fff;
      border-radius: 15px;
      position: relative;
      .title {
        margin-bottom: 25px;
        line-height: 20px;
        text-indent: 15px;
        margin-top: 15px;
        margin-left: 15px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .price {
        margin-left: 30px;
        margin-top: 50px;
        font-size: 1.5625rem;
        color: red;
      }
      .bth {
        width: 110px;
        height: 35px;
        background-color: red;
        color: #fff;
        border-radius: 10px;
        position: absolute;
        right: 15px;
        top: 80px;
      }
    }
  }

  .bodys {
    width: 100%;
    display: grid;
    background-color: whitesmoke;
  }
}
</style>